home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Gekikoh Dennoh Club 5
/
Gekikoh Dennoh Club Vol. 5 (Japan).7z
/
Gekikoh Dennoh Club Vol. 5 (Japan) (Track 01).bin
/
internet
/
tcppack
/
tcppackb.lzh
/
src
/
samples
/
ifconfig.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-08-07
|
5KB
|
252 lines
/*
* ifconfig.c - configure interfaces.
*
* Copyright (C) 1994 Tomoaki Tada/F.C.T.
*/
static char *rcsid = "$Id: ifconfig.c,v 1.4 1994/08/04 05:02:08 Niggle Exp $";
/*
* $Log: ifconfig.c,v $
* Revision 1.4 1994/08/04 05:02:08 Niggle
* inetdé¬ÅφÆôé╡é─éóé╚éóÅΩìçé╠ò\Īé≡ò╧ìX
*
* Revision 1.3 1994/07/23 01:01:19 Niggle
* Å¡üXò╧ìX
*
* Revision 1.2 1994/07/18 12:49:59 Niggle
* hostnameé┼é╠ÄwÆΦé≡ë┬ö\é╔
* netmask, broadcast é≡Ä⌐ô«ôIé╔ì∞ɼé╖éΘéµéñé╔ò╧ìX
*
* Revision 1.1 1994/06/22 01:58:08 Niggle
* Initial revision
*
*/
#include<stdio.h>
#include<string.h>
#include"network.h"
static void ifconfig (int, char **);
static void display_iface_stat (iface *);
static int config_sub (iface *, int, char **);
static long make_netmask (long);
static long make_broad_cast (long, long);
static char *num[] = {
"",
"twice",
"three times",
"many times",
};
/************************************************
* *
************************************************/
int
main (int argc, char **argv)
{
long version = _get_version ();
if (version == -1)
{
fprintf (stderr, "TCP/IP driveré═ÅφÆôé╡é─éóé▄é╣é±\n");
return -1;
}
ifconfig (argc, argv);
return 0;
}
/************************************************
* *
************************************************/
static void
ifconfig (int argc, char **argv)
{
char *i_name;
iface *p;
if (argc == 1)
return;
i_name = argv[1];
p = get_new_iface (i_name);
if (!p)
{
printf ("no such interface\n");
return;
}
if (argc == 2)
display_iface_stat (p);
else
{
/* Ä└ì█é╔Åæì₧é▐ */
config_sub (p, argc - 1, &argv[1]);
}
/* link é╡é╚é¿é╖ */
link_new_iface (p);
return;
}
/************************************************
* *
************************************************/
static void
display_iface_stat (iface *p)
{
unsigned int flag;
char ip_addr[32], broadcast[32];
flag = p->flag;
printf ("%s: flags=%x<", p->name, flag);
if (flag & IFACE_UP)
printf ("UP");
else
printf ("DOWN");
if (flag & IFACE_RUNNING)
printf (",RUNNING");
if (flag & IFACE_NOARP)
printf (",NOARP");
if (flag & IFACE_NOTRAIL)
printf (",NOTRAILERS");
if (flag & IFACE_BROAD)
printf (",BROADCAST");
if (flag & IFACE_LOOPBACK)
printf (",LOOPBACK");
if (flag & IFACE_PTP)
printf (",POINTOPOINT");
printf (">\n");
n2a_ipaddr (p->my_ip_addr, ip_addr);
n2a_ipaddr (p->broad_cast, broadcast);
printf ("\tinet %s netmask 0x%8.8x", ip_addr, p->net_mask);
if (flag & IFACE_BROAD)
printf (" broadcast %s\n", broadcast);
else
printf ("\n");
}
/************************************************
* *
************************************************/
static int
config_sub (iface * my_iface, int argc, char **argv)
{
int i, addr_p;
int up_p, down_p;
long my_ip, netmask, broadcast;
addr_p = 0;
my_ip = my_iface->my_ip_addr;
netmask = my_iface->net_mask;
broadcast = my_iface->broad_cast;
up_p = 0; down_p = 0;
for (i = 1; i < argc; i++)
{
if (!strcmp (argv[i], "up"))
up_p = 1;
else if (!strcmp (argv[i], "down"))
down_p = 1;
else if (!strcmp (argv[i], "broadcast"))
{
if (i + 1 < argc)
broadcast = a2n_ipaddr (argv[++i]);
}
else if (!strcmp (argv[i], "netmask"))
{
if (i + 1 < argc)
netmask = a2n_ipaddr (argv[++i]);
}
else
{
if (addr_p)
{
if (addr_p > 2)
addr_p = 2;
fprintf (stderr, "Warning: network address was specified %s.\n", num[addr_p]);
}
if (isipaddr (argv[i]))
my_ip = a2n_ipaddr (argv[i]);
else
{
struct hostent *h;
h = gethostbyname (argv[i]);
if (!h)
{
fprintf (stderr, "Host name(%s) look up failure.\n", argv[i]);
return 0;
}
my_ip = *(long *)h->h_addr;
}
addr_p++;
}
}
my_iface->my_ip_addr = my_ip;
if (!netmask)
netmask = make_netmask (my_ip);
my_iface->net_mask = netmask;
if (!broadcast)
broadcast = make_broad_cast (my_ip, netmask);
my_iface->broad_cast = broadcast;
if (!(my_iface->flag & IFACE_UP) && up_p && !down_p)
my_iface->flag |= IFACE_UP;
else if ((my_iface->flag & IFACE_UP) && down_p && !up_p)
my_iface->stop (my_iface);
return 0;
}
/************************************************
* *
************************************************/
static long
make_netmask (long target)
{
long tmp;
long result;
int class;
tmp = target >> 32 - 4;
for (class = 0; class < 3; class++)
{
if (!(tmp & (1 << 3 - class)))
break;
}
switch (class)
{
case 0: /* class A */
result = 0xff000000;
break;
case 1: /* class B */
result = 0xffff0000;
break;
case 2: /* class C */
result = 0xffffff00;
break;
default:
result = 0;
}
return result;
}
/************************************************
* *
************************************************/
static long
make_broad_cast (long target, long mask)
{
return (0xffffffff & ~mask) | target;
}